
$(function () {
    var workTypeList;

    $.ajax({
        url:"/getAllWorkType",
        type:"get",

        success:function(data){
            workTypeList = data
            layui.use('table', function(){
                var table = layui.table;
                var form = layui.form;

                form.verify({
                    basepay:[
                        /^[1-9]\d*$/
                        ,'请输入正整数'
                    ]
                })
                function tableRender(dataList) {
                    table.render({
                        elem: '#test'
                        ,code: 0
                        ,msg: ""
                        ,data: dataList
                        ,title: '工种目录表'

                        ,cols: [[
                            {field:'id', title:'序号', width:'8%', fixed: 'left', sort: true ,unresize: true}
                            ,{field:'skillwork', title:'技能工种', width:'25%'}
                            ,{field:'basepay', title:'基准工资', width:'25%',sort: true}
                            ,{field:'isspecial', title:'工种类型', width:'25%', sort: true}
                            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:'17%',unresize: true}
                        ]]
                        ,page: true
                    });
                }
                tableRender(workTypeList)

                $("#addworktype").click(function () {
                    layer.open({
                        type:1,
                        area:['400px','350px'],
                        title: '编辑工种目录'
                        ,content: $("#editworktype"),
                        shade: 0.5,
                        cancel: function(layero,index){
                            layer.closeAll();
                        }
                    });
                    form.val('editworktype', {
                        "skillwork": '',
                        "basepay":'',
                        "isspecial":''
                    });
                    form.on('submit(editworktype)', function(data){
                        $.ajax({
                            url:"/insertWorkType",
                            type:"post",
                            data:{
                                skillwork:data.field.skillwork,
                                basepay:data.field.basepay,
                                isspecial:data.field.isspecial,
                            },
                            dataType: 'json',
                            success:function(data){
                                layer.alert("添加成功");
                                setTimeout(function (){
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                        return false;
                    });
                })

                //监听工具条
                table.on('tool(test)', function(obj){
                    var data = obj.data;
                    workTypeId = data.id
                    if(obj.event === 'detail'){
                        layer.msg('ID：'+ data.id + ' 的查看操作');
                    } else if(obj.event === 'del'){
                        layer.confirm('确定要删除吗', function(index){
                            $.ajax({
                                url:"/deleteWorkType",
                                type:"post",
                                data:{
                                    workTypeId:workTypeId
                                },
                                success:function () {
                                    obj.del();
                                    layer.close(index);
                                    workTypeList.splice($.inArray(data,workTypeList),1)
                                    tableRender(workTypeList)
                                }
                            })
                        });
                    } else if(obj.event === 'edit'){
                        layer.open({
                            type:1,
                            area:['400px','350px'],
                            title: '编辑工种目录123'
                            ,content: $("#editworktype"),
                            shade: 0.5,
                            cancel: function(layero,index){
                                layer.closeAll();
                            }
                        });
                        $.ajax({
                            url:"/getOneWorkType",
                            type:"get",
                            data:{
                                workTypeId:workTypeId,
                            },
                            dataType: 'json',
                            success:function(data){
                                form.val('editworktype', {
                                    "skillwork": data.skillwork,
                                    "basepay":data.basepay,
                                    "isspecial":data.isspecial
                                });
                            }
                        })

                        form.on('submit(editworktype)', function(data){
                            $.ajax({
                                url:"/updateWorkType",
                                type:"post",
                                data:{
                                    skillwork:data.field.skillwork,
                                    basepay:data.field.basepay,
                                    isspecial:data.field.isspecial,
                                    workTypeId:workTypeId
                                },
                                dataType: 'json',
                                success:function(data){
                                    layer.alert("修改成功");
                                    setTimeout(function (){
                                        window.location.reload();
                                    }, 1000);
                                }
                            })
                            return false;
                        });
                    }
                });
            });
        }
    })






    $('body').click(function (e) {
        if (e.target.className == "layui-layer-shade") {
            layer.closeAll()
        }
    })
})